package com.ruoyi.web.controller.system.web;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.service.CommoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WebSite;
import com.ruoyi.system.service.IWebSiteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网址Controller
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/system/site")
public class WebSiteController extends BaseController
{
    private String prefix = "system/site";

    @Autowired
    private IWebSiteService webSiteService;
    @Autowired
    private CommoneService commoneService;
    @Autowired
    private RedisCache redisCache;
    String key = "CateNumberTask";

    /**
     * 查询网址列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebSite webSite)
    {
        startPage();
        List<WebSite> list = webSiteService.selectWebSiteList(webSite);
        return getDataTable(list);
    }

    /**
     * 导出网址列表
     */
    @Log(title = "网址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebSite webSite)
    {
        List<WebSite> list = webSiteService.selectWebSiteList(webSite);
        ExcelUtil<WebSite> util = new ExcelUtil<WebSite>(WebSite.class);
        return util.exportExcel(list, "网址数据");
    }


    /**
     * 新增保存网址
     */
    @Log(title = "网址", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebSite webSite)
    {
        String username = SecurityUtils.getUsername();
        if (UtilTool.isNull(webSite.getLogoUrl())){
            String logoUrl = UtilTool.checkUrl(webSite.getWebUrl());
            webSite.setLogoUrl(logoUrl);
        }
        webSite.setAppUserNickName(username);
        webSite.setId(commoneService.getWebId());
        webSite.setSerialNum(UtilTool.getWebSerialNum());
        webSite.setCreateTime(new Date(System.currentTimeMillis()));
        webSite.setUpdateTime(new Date(System.currentTimeMillis()));
        toAjax(webSiteService.insertWebSite(webSite));
        redisCache.setCacheObject(key, webSite, 30, TimeUnit.MINUTES);
        return AjaxResult.success();
    }


    /**
     * 修改保存网址
     */
    @Log(title = "网址", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebSite webSite)
    {
        if (UtilTool.isNull(webSite.getLogoUrl())){
            String logoUrl = UtilTool.checkUrl(webSite.getWebUrl());
            webSite.setLogoUrl(logoUrl);
        }
        webSite.setUpdateTime(new Date(System.currentTimeMillis()));
        redisCache.setCacheObject(key, webSite, 30, TimeUnit.MINUTES);
        return toAjax(webSiteService.updateWebSite(webSite));
    }

    /**
     * 删除网址
     */
    @Log(title = "网址", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        redisCache.setCacheObject(key, ids, 30, TimeUnit.MINUTES);
        return toAjax(webSiteService.deleteWebSiteByIds(ids));
    }


    /**
     * 重置网址
     */
    @Log(title = "重置网址", businessType = BusinessType.UPDATE)
    @PostMapping("/reset")
    @ResponseBody
    public AjaxResult reset(String ids)
    {
        String[] id = ids.split(",");
        for (int i = 0;i<id.length;i++){
            WebSite webSite = webSiteService.selectWebSiteById(id[i]);
            if (!UtilTool.isNull(webSite)){
                webSite.setSerialNum("0");
                webSite.setUpdateTime(new Date(System.currentTimeMillis()));
                webSiteService.updateWebSite(webSite);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 恢复网址
     */
    @Log(title = "恢复网址", businessType = BusinessType.UPDATE)
    @PostMapping("/restore")
    @ResponseBody
    public AjaxResult restore(String ids)
    {
        String[] id = ids.split(",");
        for (int i = 0;i<id.length;i++){
            WebSite webSite = webSiteService.selectWebSiteById(id[i]);
            if (!UtilTool.isNull(webSite)){
                webSite.setSerialNum(UtilTool.getWebSerialNum());
                webSite.setUpdateTime(new Date(System.currentTimeMillis()));
                webSiteService.updateWebSite(webSite);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 网址去重
     * @param webSite
     * @return
     */
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(WebSite webSite){
        WebSite site = webSiteService.getOneWebSite(webSite);
        if (!UtilTool.isNull(site)){
            return AjaxResult.error(201,"该链接已经有了，已经添加了");
        }else {
            return AjaxResult.success();
        }
    }
    /**
     * 用户网址统计
     */
    @PostMapping("/getAppUserlist")
    @ResponseBody
    public TableDataInfo getAppUserlist(WebSite webSite)
    {
        webSite.setSource(1);
        startPage();
        List<WebSite> list = webSiteService.getAppUserlist(webSite);
        return getDataTable(list);
    }

    /**
     * 通过ids查询网址列表
     */
    @PostMapping( "/getWebSiteByIds")
    @ResponseBody
    public AjaxResult getWebSiteByIds(String ids) {
        List<WebSite> list = webSiteService.getWebSiteByIds(ids);
        return AjaxResult.success(list);
    }

}
